import { gameLogin } from "@/api/casion";
import { ElNotification } from "element-plus";
import router from "@/router";
import { mainAct } from "@/store/activeda";
import { menu } from "@/store/menu";
import { mainWallet } from "@/store/wallet";

const main = mainAct();
const menuStore = menu();
const main2 = mainWallet();
// 解析问号后面的参数
const getQuery = url => {
  let obj = {};
  if (!url) return obj;
  let search = url.split("?")[1];
  if (!search) return obj;
  search = search.split("&");
  search.forEach(item => {
    let arr = item.split("=");
    obj[arr[0]] = arr[1];
  });
  return obj;
};

const BANNER_MAP = {
  LOGIN_GAME: "login_game",
  LINK: "link",
  NONE: "none",
  ACTIVITY: "active",
  RECHARGE: "recharge",
  WITHDRAW: "withdraw"
};

const ACTIVE_KEY_MAP = {
  recharge_by_day: "tab6",
  recharge_by_range: "tab7",
  bet_return_water: "tab8",
  first_recharge: "tab9",
  spin_wheel: "tab10",
  redeem_code: "tab11"
};

const strategy = {
  [BANNER_MAP.LOGIN_GAME]: data => {
    console.log("data", data, getQuery(data));
    const query = getQuery(data);
    const params = {
      game_code: query.game_code
    };
    gameLogin(params).then(res => {
      if (res.errno == 0) {
        eventBus.emit("game_enter", query.game_code);
        //跳转iframe
        router.push({ name: "Iframe", params: { 
          game_url: res.data.path,
          userToken: main.user_detail.uid
        } });
      } else if (
        res.errno == 10015 ||
        res.errno == 20005 ||
        res.errno == 10204
      ) {
        // console.log(10015);
        // ElNotification({
        //   position: "top-left",
        //   zIndex: 1004,
        //   title: "Error",
        //   message: res.msg,
        //   type: "error"
        // });
        main.loginLayer = true;
      }
    });
  },
  [BANNER_MAP.LINK]: data => {
    const query = getQuery(data);
    window.open(query.url, "_blank");
  },
  [BANNER_MAP.NONE]: data => {
    console.log("data", data);
  },
  [BANNER_MAP.RECHARGE]: data => {
    console.log("data", data);
    main2.getBankInfo("showRecharge");
  },
  [BANNER_MAP.WITHDRAW]: data => {
    console.log("showBank", data);
    main2.getBankInfo("showBank");
  },
  [BANNER_MAP.ACTIVITY]: async data => {
    const query = getQuery(data);
    if (ACTIVE_KEY_MAP[query.key]) {
      router.push({
        path: "active",
        query: {
          tab: ACTIVE_KEY_MAP[query.key]
        }
      });
    } else {
      await menuStore.getActiveCustomizeViewData;
      const find = menuStore.getCustomeMenu.find(item => item.key == query.key);
      if (find) {
        router.push({
          path: "active",
          query: {
            tab: find.query.tab
          }
        });
      }
    }
  }
};

// 策略模式
export const strategyEntry = (type, data) => {
  console.log("type", type);

  return strategy[type](data);
};
